package cn.softgame.cms.secure.organization.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.softgame.cms.common.exception.BissinessException;
import cn.softgame.cms.open.authentication.domain.SysModule;
import cn.softgame.cms.open.common.service.CommonService;
import cn.softgame.cms.secure.module.controller.ModuleController;
import cn.softgame.cms.secure.organization.domain.OrganizationBean;
import cn.softgame.cms.secure.organization.service.IOrgService;


/**
 * 组织管理配置控制器
 * @author yangying
 * 2013-4-9
 */
@Controller
@RequestMapping("/secure/org/*")
public class OrgController {

	private Logger logger = LoggerFactory.getLogger(OrgController.class);
	@Autowired(required=true)
	private IOrgService orgService;
	
	@Autowired
	private CommonService commonService;
	
	
	@RequestMapping("listOrgs.html")
	public ModelAndView listOrgs() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("org",orgService.queryOrgByOrgId("1"));
		mav.setViewName("secure/org/queOrg");
		return mav;
	}
	
	@RequestMapping("loadOrgsData.html")
	public @ResponseBody List<OrganizationBean> loadOrgsData(){
		List<OrganizationBean> list = orgService.loadOrgsData();
		// 去掉parentSysOrg
		for(OrganizationBean m : list){
			m.setParentSysOrg(null);
		}
		return list;
	}
	
	@RequestMapping("queryOrgByOrgId.html")
	public ModelAndView queryOrgByOrgId(@RequestParam String orgId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("org", orgService.queryOrgByOrgId(orgId));
		mav.setViewName("secure/org/orgForm");
		return mav;
	}
	
	@RequestMapping("addOrg.html")
	public ModelAndView addOrg(@RequestParam String parentOrgId) {
		ModelAndView mav = new ModelAndView();
		OrganizationBean parentSysOrg = orgService.queryOrgByOrgId(parentOrgId);
		OrganizationBean org = new OrganizationBean();
		org.setParentSysOrg(parentSysOrg);
		mav.addObject("org", org);
		mav.setViewName("secure/org/orgForm");
		return mav;
	}
	
	@RequestMapping(value="delete.html",produces="text/plain;charset=UTF-8")
	public @ResponseBody String delete(@RequestParam String orgId) {
		String msg = "";
		try {
			orgService.delete(orgId);
			msg = "删除成功!";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败!";
		}
		return msg;
	}
	
	/**
	 * 保存模块
	 * 注意:单独返回String有中文时,必须加上produces="text/plain;charset=UTF-8"处理中文乱码
	 * @param org
	 * @return
	 */
	@RequestMapping(value="save.html")
	public @ResponseBody Map<String,Object> save(OrganizationBean org) throws BissinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			if(org!=null){
				String orgId = org.getOrgId();
				OrganizationBean parent = org.getParentSysOrg();
				String parentOrgId = "";
				String parentTreeCode = null;
				if(parent!=null){
					parentOrgId=parent.getOrgId();
					parentTreeCode = parent.getTreeCode();
				}
				org.setParentOrgId(parentOrgId);
				// 不管是新增还是更新,都生成一个TreeCode,简化逻辑
				org.setTreeCode(commonService.getMaxTreeCode("SYS_ORGANIZATION", "tree_code", null, parentTreeCode));
				if(orgId!=null && !"".equals(orgId)){
					org.setUpdateDate(commonService.getDatabaseCurrentTime());
					orgService.update(org);
				}else{
					org.setOrgId(commonService.getCode());
					org.setCreateDate(commonService.getDatabaseCurrentTime());
					org.setIsValid("Y");
					orgService.save(org);
				}
			}
			map.put("flag", "success");
			map.put("org", org);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", "error");
			logger.error("cn.softgame.cms.secure.organization.controller.OrgController.save", e);
		}
		return map;		
	}

			
}
